package com.gkj.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gkj.blog.business.dto.PageResult;
import com.gkj.blog.business.enums.ResponseStatus;
import com.gkj.blog.business.service.ResourcesService;
import com.gkj.blog.business.vo.condition.ResourceConditionVO;
import com.gkj.blog.business.vo.entity.Resources;
import com.gkj.blog.framework.object.ResponseVO;
import com.gkj.blog.util.ResultUtil;
import com.gkj.blog.util.SessionUtil;
import com.gkj.shiro.ShiroService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/resources")
public class ResourcesController {

    @Autowired
    private ResourcesService resourcesService;
    @Autowired
    private ShiroService shiroService;

    @PostMapping("/list")
    public PageResult list(ResourceConditionVO vo) {
        PageInfo<Resources> pageInfo = resourcesService.findPageBreakByCondition(vo);
        return ResultUtil.tablePage(pageInfo);
    }
    @RequestMapping("/listAll")
    public ResponseVO listAll(){
        ResourceConditionVO vo=new ResourceConditionVO();
        vo.setPageSize(200);
        PageInfo<Resources> pageInfo = resourcesService.findPageBreakByCondition(vo);
        return ResultUtil.success(null,pageInfo==null?null:pageInfo.getList());
    }

    @PostMapping("/resourcesWithSelected")
    public ResponseVO<List<Resources>> resourcesWithSelected(Long rid) {
        return ResultUtil.success(null, resourcesService.queryResourcesListWithSelected(rid));
    }

    @PostMapping("/loadMenu")
    public List<Resources> loadMenu() {
        Long userId = SessionUtil.getUser().getId();
        return resourcesService.listUserResources(userId);
    }

    @PostMapping("/listParents")
    public List<Resources> listParents() {
        return resourcesService.listAllParentResource();
    }

//    @CacheEvict(cacheNames = "resources", allEntries = true)
    @PostMapping(value = "/add")
    public ResponseVO add(Resources resources) {
        resourcesService.insert(resources);
        //更新权限
        shiroService.updatePermission();
        return ResultUtil.success("成功");
    }

//    @CacheEvict(cacheNames = "resources", allEntries = true)
    @PostMapping(value = "/remove")
    public ResponseVO remove(@RequestParam("ids[]") Long[] ids) {
        resourcesService.deleteIds(ids);
        //更新权限
        shiroService.updatePermission();
        return ResultUtil.success("成功删除 [" + ids.length + "] 个资源");
    }

    @PostMapping("/get/{id}")
    public ResponseVO get(@PathVariable Long id) {
        return ResultUtil.success(null, this.resourcesService.select(id));
    }

    @PostMapping("/edit")
    public ResponseVO edit(Resources resources) {
        try {
            resourcesService.update(resources);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("资源修改失败！");
        }
        return ResultUtil.success(ResponseStatus.SUCCESS);
    }
}
